package com.tourscool.springboot.starter.redisson.config;

import org.redisson.connection.balancer.LoadBalancer;
import org.redisson.connection.balancer.RandomLoadBalancer;
import org.redisson.connection.balancer.RoundRobinLoadBalancer;
import org.redisson.connection.balancer.WeightedRoundRobinBalancer;

import java.util.HashMap;

/**
 * 我长得帅，不需要注释
 *
 * @author Carlton
 * @date 2018/12/27
 */
public class BaseMasterSlaveServersConfig extends BaseServerConfig {
    private String loadBalancer = "org.redisson.connection.balancer.RoundRobinLoadBalancer";
    /**
     * Redis 'slave' node minimum idle connection amount for <b>each</b> slave node
     */
    private int slaveConnectionMinimumIdleSize = 10;
    /**
     * Redis 'slave' node maximum connection pool size for <b>each</b> slave node
     */
    private int slaveConnectionPoolSize = 64;
    /**
     * Redis 'master' node minimum idle connection amount for <b>each</b> slave node
     */
    private int masterConnectionMinimumIdleSize = 10;
    /**
     * Redis 'master' node maximum connection pool size
     */
    private int masterConnectionPoolSize = 64;
    private String readMode = "SLAVE";
    private String subscriptionMode = "SLAVE";
    /**
     * Redis 'slave' node minimum idle subscription (pub/sub) connection amount for <b>each</b> slave node
     */
    private int subscriptionConnectionMinimumIdleSize = 1;
    /**
     * Redis 'slave' node maximum subscription (pub/sub) connection pool size for <b>each</b> slave node
     */
    private int subscriptionConnectionPoolSize = 50;
    private long dnsMonitoringInterval = 5000;

    /**
     * 获取LoadBalancer
     * org.redisson.connection.balancer.WeightedRoundRobinBalancer - 权重轮询调度算法
     * org.redisson.connection.balancer.RoundRobinLoadBalancer - 轮询调度算法
     * org.redisson.connection.balancer.RandomLoadBalancer - 随机调度算法
     *
     * @param name
     * @return
     */
    static LoadBalancer getLoadBalancer(String name) {
        switch (name) {
            case "org.redisson.connection.balancer.RoundRobinLoadBalancer":
                return new RoundRobinLoadBalancer();
            case "org.redisson.connection.balancer.WeightedRoundRobinBalancer":
                return new WeightedRoundRobinBalancer(new HashMap<>(), 0);
            case "org.redisson.connection.balancer.RandomLoadBalancer":
                return new RandomLoadBalancer();
            default:
                return new RoundRobinLoadBalancer();
        }
    }

    public String getLoadBalancer() {
        return loadBalancer;
    }

    public void setLoadBalancer(final String loadBalancer) {
        this.loadBalancer = loadBalancer;
    }

    public int getSlaveConnectionMinimumIdleSize() {
        return slaveConnectionMinimumIdleSize;
    }

    public void setSlaveConnectionMinimumIdleSize(final int slaveConnectionMinimumIdleSize) {
        this.slaveConnectionMinimumIdleSize = slaveConnectionMinimumIdleSize;
    }

    public int getSlaveConnectionPoolSize() {
        return slaveConnectionPoolSize;
    }

    public void setSlaveConnectionPoolSize(final int slaveConnectionPoolSize) {
        this.slaveConnectionPoolSize = slaveConnectionPoolSize;
    }

    public int getMasterConnectionMinimumIdleSize() {
        return masterConnectionMinimumIdleSize;
    }

    public void setMasterConnectionMinimumIdleSize(final int masterConnectionMinimumIdleSize) {
        this.masterConnectionMinimumIdleSize = masterConnectionMinimumIdleSize;
    }

    public int getMasterConnectionPoolSize() {
        return masterConnectionPoolSize;
    }

    public void setMasterConnectionPoolSize(final int masterConnectionPoolSize) {
        this.masterConnectionPoolSize = masterConnectionPoolSize;
    }

    public String getReadMode() {
        return readMode;
    }

    public void setReadMode(final String readMode) {
        this.readMode = readMode;
    }

    public String getSubscriptionMode() {
        return subscriptionMode;
    }

    public void setSubscriptionMode(final String subscriptionMode) {
        this.subscriptionMode = subscriptionMode;
    }

    public int getSubscriptionConnectionMinimumIdleSize() {
        return subscriptionConnectionMinimumIdleSize;
    }

    public void setSubscriptionConnectionMinimumIdleSize(final int subscriptionConnectionMinimumIdleSize) {
        this.subscriptionConnectionMinimumIdleSize = subscriptionConnectionMinimumIdleSize;
    }

    public int getSubscriptionConnectionPoolSize() {
        return subscriptionConnectionPoolSize;
    }

    public void setSubscriptionConnectionPoolSize(final int subscriptionConnectionPoolSize) {
        this.subscriptionConnectionPoolSize = subscriptionConnectionPoolSize;
    }

    public long getDnsMonitoringInterval() {
        return dnsMonitoringInterval;
    }

    public void setDnsMonitoringInterval(final long dnsMonitoringInterval) {
        this.dnsMonitoringInterval = dnsMonitoringInterval;
    }
}
